﻿using demo.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace demo
{
    public partial class BillSummaryForm : Form
    {
        public delegate void FormShowInvoke();
        public FormShowInvoke invokeShow;
        private static ServerSQL.operateSoapClient sqlServer;
        OpaqueCommand cmd = new OpaqueCommand();
        private Thread threadOrderBill;
        private UesrModel UserInfo;
        DataTable billdt;
        DataTable billsummarydt;
        public BillSummaryForm()
        {
            InitializeComponent();
           
        }
        public BillSummaryForm(UesrModel userInfo)
        {
            InitializeComponent();

            UserInfo = userInfo;
            textBox1.Text = UserInfo.user_id;

        }
        private void BillSummaryForm_Load(object sender, EventArgs e)
        {

            dateTimePicker1.Value = DateTime.Now; // 设置当前时间
            dateTimePicker2.Value = DateTime.Now; // 设置当前时间
            dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
            dateTimePicker2.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 只选择整行，不改变颜色或字体样式
            dataGridView2.AllowUserToAddRows = false;
            dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 只选择整行，不改变颜色或字体样式
            sqlServer = new ServerSQL.operateSoapClient();
            cmd.ShowOpaqueLayer(this, 100, true);
            invokeShow = updateData;
            reqbillModel reqData = new reqbillModel();
            reqData.pid = textBox1.Text;
            reqData.startDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");// "2025-03-20 00:00:00.283";
            reqData.endDate = dateTimePicker2.Value.ToString("yyyy-MM-dd");
            threadOrderBill = new Thread(new ParameterizedThreadStart(getOrderBill));
            threadOrderBill.Start(reqData); // 直接传递参数给DoWork方法
        }

        decimal OrderBillMoney = 0;

        public void getOrderBill(object obj)
        {
            OrderBillMoney = 0;
            var reqData = obj as reqbillModel;
            billdt = new DataTable();
            billdt.Columns.Add("患者ID", typeof(string));
            billdt.Columns.Add("姓名", typeof(string));
            billdt.Columns.Add("身份证号", typeof(string));
            billdt.Columns.Add("订单号", typeof(string));
            billdt.Columns.Add("流水号", typeof(string));
            billdt.Columns.Add("订单金额", typeof(string));
            billdt.Columns.Add("支付时间", typeof(string));
            billdt.Columns.Add("操作员ID", typeof(string));
            reqData.startDate = reqData.startDate + " 00:00:00.000";
            reqData.endDate = reqData.endDate + " 23:59:59.000";
            var data = sqlServer.GetOrderBill(reqData.pid, reqData.startDate, reqData.endDate);

           if (data.code == 0 && data.data.Length > 0)
           {
               OrderBillMoney = data.data.Sum(x => Convert.ToDecimal(x.orderMoney));
               foreach (var item in data.data)
               {
                   billdt.Rows.Add(item.patientId, item.PatientName, item.patientIdCardNo, item.orderNo, item.tradeNo, item.orderMoney, item.payTime, item.operateId);
               }
           }
           billsummarydt = new DataTable();
           billsummarydt.Columns.Add("操作员", typeof(string));
           billsummarydt.Columns.Add("充值笔数", typeof(string));
           billsummarydt.Columns.Add("充值总金额", typeof(string));
           var dataSummary = sqlServer.OrderBillSummary(reqData.pid, reqData.startDate, reqData.endDate);
           if (dataSummary.code == 0 && dataSummary.data.Length > 0)
           {
               foreach (var item in dataSummary.data)
               {
                   billsummarydt.Rows.Add(item.operateId, item.payCount, item.payMoney);
               }
           }
           if (this.Created)
           {
               this.BeginInvoke(this.invokeShow);
           }
        }

        private void updateData()
        {
            label4.Text = "总笔数：" + billdt.Rows.Count + "               总金额：" + OrderBillMoney;

            dataGridView1.DataSource = billdt;
            int indexs = 0;
            dataGridView1.Columns[indexs].HeaderText = "患者ID";
            dataGridView1.Columns[indexs].Width = 80;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "姓名";
            dataGridView1.Columns[indexs].Width = 80;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "身份证号";
            dataGridView1.Columns[indexs].Width = 120;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "订单号";
            dataGridView1.Columns[indexs].Width = 150;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "流水号";
            dataGridView1.Columns[indexs].Width = 200;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "订单金额";
            dataGridView1.Columns[indexs].Width = 80;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "支付时间";
            dataGridView1.Columns[indexs].Width = 150;
            indexs++;
            dataGridView1.Columns[indexs].HeaderText = "操作员ID";
            dataGridView1.Columns[indexs].Width = 80;


            dataGridView2.DataSource = billsummarydt;
            int index = 0;
            dataGridView2.Columns[index].HeaderText = "操作员";
            dataGridView2.Columns[index].Width = 100;

            index++;
            dataGridView2.Columns[index].HeaderText = "充值笔数";
            dataGridView2.Columns[index].Width = 100;

            index++;
            dataGridView2.Columns[index].HeaderText = "充值总金额";
            dataGridView2.Columns[index].Width = 100;

            cmd.HideOpaqueLayer();

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cmd.ShowOpaqueLayer(this, 100, true);
                reqbillModel reqData = new reqbillModel();
                reqData.pid = textBox1.Text;
                reqData.startDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                reqData.endDate = dateTimePicker2.Value.ToString("yyyy-MM-dd");
                threadOrderBill = new Thread(new ParameterizedThreadStart(getOrderBill));
                threadOrderBill.Start(reqData); // 直接传递参数给DoWork方法
            }
        }


        public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            cmd.ShowOpaqueLayer(this, 100, true);
            reqbillModel reqData = new reqbillModel();
            reqData.pid = textBox1.Text;
            reqData.startDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            reqData.endDate = dateTimePicker2.Value.ToString("yyyy-MM-dd");
            threadOrderBill = new Thread(new ParameterizedThreadStart(getOrderBill));
            threadOrderBill.Start(reqData); // 直接传递参数给DoWork方法
        }
    }


    public class reqbillModel
    {
        public string pid { get; set; }
        public string startDate { get; set; }
        public string endDate { get; set; }
    }

}
